<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<el-form :model="addForm"  ref="addForm" class="demo-ruleForm">
				<el-row :gutter="40">
					<el-col :span="8">
						<div class="text-formList">
							<span class="text_left">采购号：</span>
							<span class="text_right">{{ addForm.procureCode }}</span>
						</div>
						<div class="text-formList">
							<span class="text_left">采购类型：</span>
							<span class="text_right">{{ addForm.procureTypeCn }}</span>
						</div>
						<div class="text-formList">
							<span class="text_left">大类：</span>
							<span class="text_right">{{ addForm.processName }}</span>
						</div>
					</el-col>

					<el-col :span="8">
						<div class="text-formList"></div>
						<div class="text-formList">
							<span class="text_left">采购日期：</span>
							<span class="text_right">{{ addForm.procureTime }}</span>
						</div>
						<div class="text-formList">
							<span class="text_left">供应商：</span>
							<span class="text_right">{{ addForm.supplier }}</span>
						</div>
					</el-col>
					<el-col :span="8">
						<div class="text-formList"></div>
						<div class="text-formList">
							<span class="text_left">状态：</span>
							<span class="text_right">{{ addForm.procureStatusCn }}</span>
						</div>
						<div class="text-formList">
							<span class="text_left">采购人：</span>
							<span class="text_right">{{ addForm.procureUser }}</span>
						</div>
					</el-col>
				</el-row>
				<div class="tableList" style="margin-top: 0;">
					<table border="1">
						<tr>
							<th>序号</th>
							<th>品类</th>
							<th>物料代码</th>
							<th>物料名称</th>
							<th>品牌规格</th>
							<th>计量单位</th>
							<th>数量</th>
							<th>已入库</th>
							<th>待入库</th>
						</tr>
						<tr v-if="procureStorages.length == 0" class="noData">
							<td colspan="9">暂无数据</td>
						</tr>
						<tr v-else class="add_Table" v-for="(item, index) in procureStorages" :key="index">
							<td><p>{{ index + 1 }}</p></td>
							<td><p>{{ item.itemAlias }}</p></td>
							<td><p>{{ item.materialCode }}</p></td>
							<td><p>{{ item.materialName }}</p></td>
							<td><p>{{ item.materialGrade }}</p></td>
							<td><p>{{ item.measureUnit }}</p></td>
							<td><p>{{ item.procureCount }}</p></td>
							<td><p>{{ item.procureExistCount }}</p></td>
							<td><p>{{ item.procureLeftCount }}</p></td>
						</tr>
					</table>
				</div>
				<div class="nav_list">
					<div class="_title"><i class="iconfont icon-daifahuo"></i>入库记录</div>
					<div class="tableList" style="margin-top: 0;">
						<table border="1">
							<tr>
								<th>入库号</th>
								<th>入库时间</th>
								<th>入库数量</th>
								<th>备注</th>
								<th>入库人</th>
							</tr>
							<tr v-if="storageHistorys.length == 0" class="noData">
								<td colspan="5">暂无数据</td>
							</tr>
							<tr v-else class="add_Table" v-for="(item, index) in storageHistorys" :key="index">
								<td><p>{{item.procureStorageCode}}</p></td>
								<td><p>{{item.createTimeFt}}</p></td>
								<td><p>{{item.storageCount}}</p></td>
								<td><p>{{item.remark}}</p></td>
								<td><p>{{item.storageUserName}}</p></td>
							</tr>
						</table>
					</div>
				</div>
				<div class="nav_list" v-if="addForm.procureStatus == '0'||addForm.procureStatus == '1'">
					<div class="_title"><i class="iconfont icon-gift-card-o"></i>入库登记</div>
					<div class="tableList" style="margin-top: 0;">
						<table border="1">
							<tr>
								<th>物料代码</th>
								<th>物料名称</th>
								<th>待入库</th>
								<th>入库数量</th>
								<th>备注</th>
							</tr>
							<tr class="add_Table" v-for="(item, index) in inProcureStorages" :key="index">
								<td><p>{{ item.materialCode }}</p></td>
								<td><p>{{ item.materialName }}</p></td>
								<td><p style="color: #f44336">{{ item.procureLeftCount }}</p></td>
								<td>
									<el-input clearable v-model="item.storageCount" placeholder=""
											  :disabled=" type == 'see'"></el-input>
								</td>
								<td>
									<el-input clearable v-model="item.remark" placeholder=""
											  :disabled=" type == 'see'"></el-input>
								</td>
							</tr>
						</table>
					</div>
				</div>
			</el-form>
		</div>
	</div>
</template>
<script>
	import {getStore} from "@/plugins/localStore";
	import {formatDate} from "@/plugins/formatDate";

	export default {
		props: ['editData', 'type'],
		data() {
			return {
				addForm: {
					id:''
				},
				count:0,
				procureStorages:[],
				storageHistorys:[],
				inProcureStorages:[]
			}
		},
		methods: {
			getProcureStorageHistoryList() {
				let self = this;
				self.$http({
					url: "/jinl/procureStorage/getProcureStorageHistoryListByPid",
					method: "post",
					params:{id:self.addForm.id}
				}).then(resp => {
					if (resp.success) {
						self.storageHistorys =  JSON.parse(JSON.stringify(resp.result));
						return;
					}
					self.$message.error(resp.message)
				});
			},

			batchInsertProcureStorageHis() {
				let self = this;
				let inProcureStorages = [];
				for(let data of self.inProcureStorages){
					if(!data.storageCount){
						continue;
					}
					let copy = Object.assign({},data);
					copy.storageUserName = getStore().userName;
					inProcureStorages.push(copy);
				}
				if(inProcureStorages.length == 0){
					self.$message.warning('请输入入库数量');
					return;
				}
				if (self.count == 0) {
					self.$http({
						url: '/jinl/procureStorage/batchInsertProcureStorageHis',
						method: "post",
						data: JSON.stringify(inProcureStorages),
						dataType: 'json',
						contentType: "application/json",
					}).then(resp => {
						if (resp.success) {
							self.$notify({title: '提示', message: "操作成功！", type: 'success'});
							self.count--;
							self.$emit("refreshPage");
							return;
						}
						self.$notify({title: '提示', message: resp.message, type: 'error'});
						self.count--;
					});
					self.count++;
				}
			},

		},
		watch:{
			editData(val) {
				console.log('======editData=========')
				console.log(val)
				this.count = 0;
				this.procureStorages = JSON.parse(JSON.stringify(val.procureStorages||[]));
				this.inProcureStorages = [];
				for(let data of this.procureStorages){
					let procureCount = data.procureCount||'0';
					let procureExistCount = data.procureExistCount||'0';
					let procureLeftCount = parseFloat(procureCount) - parseFloat(procureExistCount);
					data.procureLeftCount = procureLeftCount.toFixed(2);
					if(procureLeftCount > 0){
						data.procureStorageId =  data.id;
						data.storageTotalCount =  procureCount;
						this.inProcureStorages.push(data);
					}
				}
				console.log(this.inProcureStorages)
				this.addForm = val;
				this.getProcureStorageHistoryList();
			},
		},

		mounted() {
		},
		components: {},
	}
</script>
<style lang="scss" scoped>
	@import "@/style/dialog.scss";

	.dc {
		width: 100px;
		background: #e6a23c;
	}

	.nav_list {
		margin-top: 20px;

		._title {
			line-height: 40px;
			color: #00D1D0;

			i {
				font-size: 16px;
				margin-right: 5px;
			}
		}
	}
</style>